package com.example.souvenir.product.config;

import com.example.souvenir.product.interceptor.mybatis.InsertUpdateTimeInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * mybatis的配置类
 *
 * @author hzx
 * @version 0.0.1
 */
@Configuration
@MapperScan("com.example.souvenir.product.mapper")
public class MybatisConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    public MybatisConfiguration() {
        System.out.println("创建配置类对象：MybatisConfiguration");
    }

    @PostConstruct // 使得此方法在调用了构造方法、完成了属性注入之后自动执行
    public void addInterceptor() {
        InsertUpdateTimeInterceptor interceptor = new InsertUpdateTimeInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}
